package net.lab1024.sa.admin.module.basedata.customer.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.admin.module.basedata.customer.domain.dto.CustomerDTO;
import net.lab1024.sa.admin.module.basedata.customer.domain.entity.Customer;
import net.lab1024.sa.admin.module.basedata.customer.domain.form.CustomerQueryForm;
import net.lab1024.sa.admin.module.basedata.customer.mapper.CustomerMapper;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.exception.BusinessException;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class CustomerService {

    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private CustomerBankService customerBankService;
    @Autowired
    private CustomerLicenseService customerLicenseService;
    @Autowired
    private CustomerContactServiceImpl customerContactService;

    public PageResult<Customer> getAllCustomers(CustomerQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<Customer> list = customerMapper.getAllCustomers(queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    public CustomerDTO getCustomerById(Long id) {
        if (Objects.isNull(id)) {
            throw new BusinessException("查询客户数据,id不能为空");
        }
        CustomerDTO customerDTO = new CustomerDTO();
        Customer customer = customerMapper.selectById(id);
        customerDTO.setCustomer(customer);

        customerDTO.setCustomerBankList(customerBankService.getCustomerBankByCustomerCode(customer.getCustomerCode()));
        customerDTO.setCustomerContactList(customerContactService.getCustomerContactByCustomerCode(customer.getCustomerCode()));
        customerDTO.setCustomerLicense(customerLicenseService.getCustomerLicenseByCustomerCode(customer.getCustomerCode()));
        return customerDTO;
    }

    public Customer getCustomerByCode(String customerCode) {
        if (Objects.isNull(customerCode)) {
            throw new BusinessException("查询客户数据,编码不能为空");
        }
        LambdaQueryWrapper<Customer> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Customer::getCustomerCode, customerCode);
        return customerMapper.selectOne(wrapper);
    }

    public void saveOrUpdateCustomer(CustomerDTO customerDTO) {

        if (Objects.isNull(customerDTO) || Objects.isNull(customerDTO.getCustomer())) {
            throw new BusinessException("保存客户数据,数据不能为空！");
        }
        Customer customer = customerDTO.getCustomer();
        if (customer.getCustomerId() == null) {
            customer.setCreatedDate(new Date());
            customer.setLastUpdatedDate(new Date());
            customerMapper.insertOrUpdate(customer);

        } else {
            customerMapper.updateById(customer);
        }
    }

}
